package com.xinqi.modules.exam.paper.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperEntity;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperStatsEntity;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperStatsQueryDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperStatsDataDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperStatsResultDTO;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperStatsManager;
import com.xinqi.modules.exam.paper.mapper.CourseExamPaperStatsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 课程测评统计管理层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Service
@RequiredArgsConstructor
public class CourseExamPaperStatsManagerImpl extends BaseServiceImpl<CourseExamPaperStatsMapper, CourseExamPaperStatsEntity> implements CourseExamPaperStatsManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CourseExamPaperStatsResultDTO> findPage(Page<Object> page, CourseExamPaperStatsQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CourseExamPaperStatsResultDTO> findList(CourseExamPaperStatsQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 课程测评统计数据
     */
    @Override
    public CourseExamPaperStatsEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    @Override
    public CourseExamPaperStatsEntity findByExamId(Long examId) {
        if (examId == null) {
            return null;
        }
        LambdaQueryWrapper<CourseExamPaperStatsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseExamPaperStatsEntity::getExamPaperId, examId);
        return getOne(wrapper);
    }

    /**
     * 根据id更新 课程测评统计数据，重写
     */
    @Override
    public boolean updateById(CourseExamPaperStatsEntity entity) {
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    /**
     * 根据id删除 课程测评统计数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initConfig(CourseExamPaperEntity entity) {
        CourseExamPaperStatsEntity config = new CourseExamPaperStatsEntity();
        config.setExamPaperId(entity.getId());
        this.baseMapper.insert(config);
    }

    /**
     * 更新总人数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTotalNumById(Long examId, Integer totalNum) {
        this.baseMapper.updateTotalNumById(examId, totalNum);
    }

    /**
     * 添加提交人数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddSubmittedNumById(Long examId, Integer num) {
        this.baseMapper.updateAddSubmittedNumById(examId, num);
    }

    /**
     * 添加批改人数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddApprovedNumById(Long examId, Integer num) {
        this.baseMapper.updateAddApprovedNumById(examId, num);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initStatsData(Long examId, CourseExamPaperStatsDataDTO stats01) {
        CourseExamPaperStatsEntity entity = findByExamId(examId);
        if (entity == null) {
            return;
        }
        entity.setTotalNum(stats01.getTotalNum());
        entity.setSubmittedNum(stats01.getSubmittedNum());
        entity.setApprovedNum(stats01.getApprovedNum());
        updateById(entity);
    }
}
